package dmsxl.lianbiao;

import java.awt.*;

/**
 * Author: Zhang Dongwei
 * Date: 2023/4/7 16:11
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 */
public class lb3_206 {

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(5);
        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        ListNode res = reverseList(node);
        while (res != null){
            System.out.println(res.val);
            res = res.next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null){
            ListNode currnext = curr.next;
            curr.next = pre;
            pre = curr;
            curr = currnext;
        }
        return pre;
    }
}
